//
// Created by Lenovo on 2023/3/7.
//

/* 洛谷 P1616 */

#include <stdio.h>
#include <memory.h>

#define Max(x, y) (x) > (y) ? (x) : (y)
#define int long long

signed main()
{
    int n, m;
    scanf("%lld %lld", &n, &m);
    int t[m], v[m];
    int dp[n + 1];
    memset(dp, 0, sizeof(int) * (n + 1));
    for (int i = 0; i < m; i++)
    {
        scanf("%lld %lld", &t[i], &v[i]);
    }

    for (int i = 0; i < m; i++)
    {
        for (int j = t[i]; j <= n; j++)
        {
            dp[j] = Max(dp[j], dp[j - t[i]] + v[i]);
        }
    }

    printf("%lld", dp[n]);

    return 0;
}